const mosca = require('mosca');

var ascoltatore = {
  //using ascoltatore
  type: 'mongo',
  url: 'mongodb://127.0.0.1:27017/mqtt',
  pubsubCollection: 'ascoltatori',
  mongo: {}
};

var settings = {
  host: '192.168.1.100',
  port: 1883,
  backend: ascoltatore
};

var server = new mosca.Server(settings);

server.on('clientConnected', function (client) {
  console.log('客户连接：', client.id);
});

server.on('ready', function () {
  console.log('Mosca 服务，run');


});

server.on('errror', function (err) {
  console.log('Error occured:' + err.toString());
});

server.on('published', function (packet, client) {
  if (packet.topic == 'c1') {
    console.log('Published:', packet.payload.toString());
    server.publish({topic: 'c1', payload: 'sssss'});
  }
});

module.exports = mosca
